﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace mediaPrime.Controls
{
    public class MPContentLoadAnimation : ContentControl
    {
        public bool AllowAnimation
        {
            get { return (bool)GetValue(AllowAnimationProperty); }
            set { SetValue(AllowAnimationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for AllowAnimation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AllowAnimationProperty =
            DependencyProperty.Register("AllowAnimation", typeof(bool), typeof(MPContentLoadAnimation), new UIPropertyMetadata(true));



        public MPContentLoadAnimation()
        {
            this.Focusable = false;
            this.DefaultStyleKey = typeof(MPContentLoadAnimation);

            this.Loaded += MetroContentControl_Loaded;
            this.Unloaded += MetroContentControl_Unloaded;
            this.IsVisibleChanged += MPContentLoadAnimation_IsVisibleChanged;
        }

        void MPContentLoadAnimation_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue == true)
            {
                VisualStateManager.GoToState(this, "AfterLoaded", true);
            }
        }

        public void PerformAnimation(bool unload)
        {
            if (!unload)
                VisualStateManager.GoToState(this, "AfterLoaded", true);
            else
                VisualStateManager.GoToState(this, "AfterUnLoaded", false);
        }

        private void MetroContentControl_Unloaded(
            object sender, RoutedEventArgs e)
        {
            if (AllowAnimation)
                VisualStateManager.GoToState(this, "AfterUnLoaded", false);
        }

        private void MetroContentControl_Loaded(
            object sender, RoutedEventArgs e)
        {
            if (AllowAnimation)
                VisualStateManager.GoToState(this, "AfterLoaded", true);


        }
    }
}
